package com.liferay.controller;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.portlet.bind.annotation.ActionMapping;
import org.springframework.web.portlet.bind.annotation.RenderMapping;

import com.liferay.model.Classes;
import com.liferay.model.Faculty;
import com.liferay.model.Group;
import com.liferay.model.Subject;
import com.liferay.portal.kernel.servlet.SessionErrors;
import com.liferay.portal.kernel.servlet.SessionMessages;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.service.ClassService;
import com.liferay.service.FacultyService;
import com.liferay.service.impl.ClassServiceImpl;
import com.liferay.service.impl.FacultyServiceImpl;

@Controller(value = "ManagerFacultyClassRoom")
@RequestMapping("VIEW")
public class ManagerFacultyClassRoom {

	private static Logger log = Logger.getLogger(ManagerFacultyClassRoom.class);
	private FacultyService facultyService;
	private ClassService classService;
	private List<Faculty> listFacluty;
	private List<Faculty> listFaculty;
	private List<Classes> listClass;
	private long idFaculty=0;
	private long idFacultyClass=0;
	private long idClass=0;
	
	private List<Subject> listSubject;
	private long idSubject = 0;
	private String nameSubject;
	private int numberOfCredit;
	
	@RenderMapping
	public String getPageDefault(RenderRequest request,
			RenderResponse response, Model model) {
		return "view";
	}

	// Quan li khoa

	@RenderMapping(params = "action=faculty")
	public String getPageManagerFaculty(RenderRequest request,
			RenderResponse response, Model model) {
		facultyService = new FacultyServiceImpl();
		listFacluty = facultyService.getAllFaculty();
		model.addAttribute("listFaculty", listFacluty);
		return "faculty/view";
	}

	// add khoa
	@RenderMapping(params = "action=addFacluty")
	public String addFaculty(RenderRequest request, RenderRequest response,Model model) {
		facultyService = new FacultyServiceImpl();
		List<Group> list = facultyService.getGroup();
		model.addAttribute("listGroup", list);
		return "faculty/add";
	}
	
	@ActionMapping(params = "action=saveaddFaculty")
	public void saveaddFaculty(ActionRequest request, ActionResponse response) throws UnsupportedEncodingException {
		request.setCharacterEncoding("UTF8");
		String nameFaculty = ParamUtil.getString(request, "nameFaculty");
		int group = ParamUtil.getInteger(request, "group");
		facultyService = new FacultyServiceImpl();
		Faculty faculty = new Faculty();
		faculty.setNameFaculty(nameFaculty);
		faculty.setGroup(group);
		facultyService.addFaculty(faculty);
		response.setRenderParameter("action", "faculty");
	}
	
	
	@ActionMapping(params = "action=editFaculty")
	public void editFaculty(ActionRequest request, ActionResponse response) {
		idFaculty = ParamUtil.getLong(request, "idFaculty");
		response.setRenderParameter("action", "showeditFaculty");
	}
	
	@RenderMapping(params = "action=showeditFaculty")
	public String showEditFaculty(RenderRequest request, RenderRequest response,Model model) {
		for(Faculty s : listFacluty){
			if(s.getIdFaculty()==idFaculty){
				model.addAttribute("nameFaculty", s.getNameFaculty());
				break;
			}
		}
		facultyService = new FacultyServiceImpl();
		List<Group> list = facultyService.getGroup();
		model.addAttribute("listGroup", list);
		return "faculty/edit";
	}
	
	@ActionMapping(params = "action=saveditFaculty")
	public void saveeditFaculty(ActionRequest request, ActionResponse response) {
		String nameFaculty = ParamUtil.getString(request, "nameFaculty");
		int group = ParamUtil.getInteger(request, "group");
		Faculty faculty = new Faculty();
		faculty.setIdFaculty(idFaculty);
		faculty.setNameFaculty(nameFaculty);
		faculty.setGroup(group);
		facultyService = new FacultyServiceImpl();
		facultyService.editFaculty(faculty);
		response.setRenderParameter("action", "faculty");
	}
	
	@ActionMapping(params = "action=deleteFaculty")
	public void deleteFaculty(ActionRequest request, ActionResponse response) {
		long idFaculty = ParamUtil.getLong(request, "idFaculty");
		Faculty faculty = new Faculty();
		faculty.setIdFaculty(idFaculty);
		facultyService = new FacultyServiceImpl();
		facultyService.deleteFaculty(faculty);
		response.setRenderParameter("action", "faculty");
	}
	
	// back
	
	
	
	// add, edit class
	
	
	@RenderMapping(params = "action=class")
	public String getPageManagerClass(RenderRequest request,
			RenderResponse response, Model model) {
		facultyService = new FacultyServiceImpl();
		listFacluty = facultyService.getAllFaculty();
		model.addAttribute("listFaculty", listFacluty);
		classService = new ClassServiceImpl();
		if(idFacultyClass==0){
			idFacultyClass=listFacluty.get(0).getIdFaculty();
		}
		model.addAttribute("idFacultySeleted", idFacultyClass);
		listClass = classService.getListClassByFaculty(idFacultyClass);
		model.addAttribute("listClass", listClass);
		return "classes/view";
	}
	
	// search
	@ActionMapping(params = "action=searchclass")
	public void searchClass(ActionRequest request, ActionResponse response) {
		idFacultyClass = ParamUtil.getLong(request, "idFaculty");
		response.setRenderParameter("action", "class");
	}
	
	// delete class
	
	@ActionMapping(params = "action=deleteclass")
	public void deleteClass(ActionRequest request, ActionResponse response) {
		idClass = ParamUtil.getLong(request, "idClass");
		classService = new ClassServiceImpl();
		Classes cla = new Classes();
		cla.setIdClass(idClass);
		classService.deleteClass(cla);
		response.setRenderParameter("action", "class");
	}
	
	
	// edit class
	
	@ActionMapping(params = "action=editclass")
	public void editClass(ActionRequest request, ActionResponse response) {
		idClass = ParamUtil.getLong(request, "idClass");
		response.setRenderParameter("action", "showeditclass");
	}
	
	@RenderMapping(params = "action=showeditclass")
	public String showEditClass(RenderRequest request,
			RenderResponse response, Model model) {
		model.addAttribute("listFacluty", listFacluty);
		for(Classes l : listClass){
			if(l.getIdClass()==idClass){
				model.addAttribute("nameClass", l.getNameClass());
				model.addAttribute("fkFacultyClass", l.getFkFaculty());
				break;
			}
		}
		return "classes/edit";
	}
	
	@ActionMapping(params = "action=saveeditclass")
	public void saveeditClass(ActionRequest request, ActionResponse response) {
		long idFac = ParamUtil.getLong(request, "idFaculty");
		String nameClass = ParamUtil.getString(request, "nameClass");
		classService = new ClassServiceImpl();
		Classes cla = new Classes();
		cla.setFkFaculty(idFac);
		cla.setNameClass(nameClass);
		cla.setIdClass(idClass);
		classService.updateClass(cla);
		response.setRenderParameter("action", "class");
	}
	
	@RenderMapping(params = "action=addclass")
	public String addClass(RenderRequest request,RenderResponse response, Model model) {
		model.addAttribute("listFacluty", listFacluty);
		return "classes/add";
	}
	
	@ActionMapping(params = "action=saveaddclass")
	public void saveaddClass(ActionRequest request, ActionResponse response) {
		long idFac = ParamUtil.getLong(request, "idFaculty");
		String nameClass = ParamUtil.getString(request, "nameClass");
		classService = new ClassServiceImpl();
		Classes cla = new Classes();
		cla.setFkFaculty(idFac);
		cla.setNameClass(nameClass);
		classService.addClass(cla);
		response.setRenderParameter("action", "class");
	}
	
	// subject
		@RenderMapping(params = "action=subject")
		public String getPageManagerSubject(RenderRequest renderRequest,
				RenderResponse renderResponse, Model model) {
			facultyService = new FacultyServiceImpl();
			listFaculty = facultyService.getAllFaculty();
			log.info(listFaculty);
			model.addAttribute("listFaculty", listFaculty);

			// show subject by faculty
			log.info("show Sub by Fac");
			facultyService = new FacultyServiceImpl();
			listSubject = facultyService.getSubjectByFaculty(idFaculty);
			model.addAttribute("listSubject", listSubject);
			log.info(listSubject);
			model.addAttribute("idFaculty", idFaculty);
			return "subject/view";
		}

		@ActionMapping(params = "action=showSubjectByFaculty")
		public void showSubjectByFaculty(ActionRequest actionRequest,
				ActionResponse actionResponse) {
			idFaculty = ParamUtil.getLong(actionRequest, "idFaculty");
			log.info("Subject by faculty = " + idFaculty);
			actionResponse.setRenderParameter("action", "subject");
			log.info("-----------------------------------------------");
		}

		// show page add new subject by faculty
		@RenderMapping(params = "action=showPageInsertNewSubjectByFaculty")
		public String showPageInsertNewSubjectByFaculty(
				RenderRequest renderRequest, RenderResponse renderResponse,
				Model model) {
			facultyService = new FacultyServiceImpl();
			listFaculty = facultyService.getAllFaculty();
			model.addAttribute("listFaculty", listFaculty);
			model.addAttribute("idFaculty", idFaculty);
			log.info(listFaculty);
			log.info("-----------------------------------------------");
			return "subject/add";
		}

		// delete subject by id
		@ActionMapping(params = "action=deleteCheckedSubjectById")
		public void deleteCheckedSubjectByFaculty(ActionRequest actionRequest,
				ActionResponse actionResponse) {
			try {
				idSubject = ParamUtil.getLong(actionRequest, "idSubject");
				log.info("Delete checked Subject By Faculty with id " + idSubject);
				facultyService = new FacultyServiceImpl();
				facultyService.deleteSubjectById(idSubject);
				log.info("SUCCESS");
				SessionMessages.add(actionRequest.getPortletSession(), "success");
				actionResponse.setRenderParameter("action", "subject");
				log.info("-----------------------------------------------");
			} catch (Exception e) {
				e.printStackTrace();
				SessionErrors.add(actionRequest.getPortletSession(), "error");
			}
		}

		// add subject by faculty
		@ActionMapping(params = "action=insertSubjectByFaculty")
		public void insertSubjectByFaculty(ActionRequest actionRequest,
				ActionResponse actionResponse) {
			try {
				String nameSubject = ParamUtil.getString(actionRequest,
						"nameSubject");
				int numberOfCredit = ParamUtil.getInteger(actionRequest,
						"numberOfCredit");
				long idFaculty = ParamUtil.getLong(actionRequest, "idFaculty");
				log.info("Insert Subject By Faculty =" + idFaculty);
				Subject subject = new Subject();
				subject.setNameSubject(nameSubject);
				subject.setNumberOfCredit(numberOfCredit);
				subject.setFkFaculty(idFaculty);
				facultyService = new FacultyServiceImpl();
				facultyService.insertSubjectByFaculty(subject);
				log.info("SUCCESS");
				SessionMessages.add(actionRequest.getPortletSession(), "success");
				actionResponse.setRenderParameter("action", "subject");
				log.info("-----------------------------------------------");
			} catch (Exception e) {
				e.printStackTrace();
				SessionErrors.add(actionRequest.getPortletSession(), "fail");
			}
		}

		// edit subject by id
		@ActionMapping(params = "action=viewPageSubjectByFaculty")
		public void viewPageSubjectByFaculty(ActionRequest actionRequest,
				ActionResponse actionResponse) {
			idSubject = ParamUtil.getLong(actionRequest, "idSubject");
			log.info("Id subject " + idSubject);
			actionResponse.setRenderParameter("action", "handleEditPage");
		}

		@RenderMapping(params = "action=handleEditPage")
		public String handleEditPage(RenderRequest renderRequest,
				RenderResponse renderResponse, Model model) {
			facultyService = new FacultyServiceImpl();
			listSubject = new ArrayList<Subject>();
			listSubject = facultyService.getSubjectById(idSubject);
			model.addAttribute("listSubject", listSubject);
			log.info(listSubject);
			return "subject/edit";
		}

		@ActionMapping(params = "action=updateSubject")
		public void updateSubject(ActionRequest actionRequest,
				ActionResponse actionResponse) {
			try {
				log.info("UPDATING...");
				idSubject = ParamUtil.getLong(actionRequest, "idSubject");
				numberOfCredit = ParamUtil.getInteger(actionRequest,
						"numberOfCredit");
				nameSubject = ParamUtil.getString(actionRequest, "nameSubject");

				facultyService = new FacultyServiceImpl();
				facultyService.updateSubjectById(nameSubject, numberOfCredit,
						idSubject);

				log.info("UPDATE SUCCESS");
				SessionMessages.add(actionRequest.getPortletSession(),
						"success");
				actionResponse.setRenderParameter("action", "subject");
			} catch (Exception e) {
				SessionErrors
						.add(actionRequest.getPortletSession(), "fail");
				e.printStackTrace();
			}

		}

}
